<template>
	<view class="cenbox">
		<!-- #ifdef H5 -->
		<uni-nav-bar title="门店">
		</uni-nav-bar>
		<!-- #endif -->
		<view class="shopnamelist">
			<scroll-view :scroll-y="true" v-if="ShopNameList.length!=0">
				<view class="shopnamelist_center" v-for="(item,index) in ShopNameList" @click.stop="shopcut(item,index)"
					:key="index">
					{{item.physical_name}}
				</view>
			</scroll-view>
			<view class="none" v-else>
				未绑定门店！
			</view>
			<view style="display: flex;justify-content: space-between;">
				<view class="btn-bind" @click.stop="bind">
					绑定
				</view>
				<view class="button" @click.stop="onenceshi">
					关闭
				</view>
			</view>
		</view>
		<uni-popup type="center" ref="bind">
			<view class="cancel-box">
				<view class="popup-open">绑定门店</view>
				<input class="reason-input" v-model="storeName" type="text" placeholder="请输入门店名称" />
				<view class="buttons">
					<view style="color: #000;" @click="close">取消</view>
					<view style="color: #fff;background-color: #ff0000ba;" @click="confirm">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		name: 'Merchant',
		props: {},
		data() {
			return {
				ShopNameList: [],
				shopindex: 0,
				shopname: '切换门店',
				physical_id: '',
				storeName: ''
			}
		},
		mounted() {
			this.shopList()
			this.physical_id = uni.getStorageSync('physical_id')
		},
		methods: {
			shopcut(e, index) {
				if (this.physical_id == e.physical_id) {
					uni.showToast({
						icon: "none",
						title: "此为当前绑定门店！"
					})
					return
				}
				this.physical_id = e.physical_id
				
				
				
				this.$util.request({
					url: '/mobile/index.php?act=member_index&op=set_physical',
					method: 'POST',
					data: {
						physical_id: this.physical_id
					}
				}).then(res => {
					if (res.error_code == 0) {
						// uni.setStorage({
						// 	key:"share_phyid",
						// 	data:this.physical_id
						// })
						uni.showToast({
							icon: "success",
							title: "切换成功！"
						})
						this.$emit('fnChange')
					}
				}).catch(err => {
					uni.showToast({
						icon: "error",
						title: "切换失败！"
					})
					this.$emit('fnChange')
				})
			},
			onenceshi() {
				setTimeout(() => {
					this.$emit('fnChange')
				}, 500)
			},
			// 获取绑定门店列表 
			shopList() {
				this.$util.request({
						url: '/mobile/index.php?act=member_index&op=physical_list',
						method: 'POST',
						data: {}
					}).then(res => {
						if (res.error_code == 0) {
							console.log('门店列表', res.datas)
							this.ShopNameList = res.datas.list
						}
					})
					.catch(res => {
						this.ShopNameList = []
					})
			},
			//绑定门店
			bind() {
				this.$refs.bind.open()
			},
			close() {
				this.$refs.bind.close()
			},
			confirm() {
				this.$util.request({
					url: '/mobile/index.php?act=member_index&op=bind_physical',
					method: 'POST',
					data: {
						phy_name: this.storeName
					}
				}).then(res => {
					if (res.error_code == 0) {
						uni.showToast({
							icon: "success",
							title: "绑定成功！",
							duration: 2000
						})
						setTimeout(() => {
							this.$emit('fnChange')
							this.$refs.bind.close()
						}, 2000)
					} else {
						uni.showToast({
							icon: "error",
							title: res.message
						})
						setTimeout(() => {
							this.$refs.bind.close()
						}, 500)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cenbox {
		width: 750rpx;
		height: 100vh;
		background: rgba(42, 31, 31, 0.5);
		backdrop-filter: blur(6px);
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
	}

	.shopnamelist {
		background: #FFFFFF;
		box-shadow: 0px 0px 24rpx 0px rgba(0, 0, 0, 0.12);
		padding: 20rpx;
		display: flex;
		flex-direction: column;
	}

	.shopnamelist_center {
		font-size: 28rpx;
		font-family: PingFang-SC-Regular, PingFang-SC;
		font-weight: 400;
		color: #3F3838;
		padding: 14rpx 0 16rpx 24rpx;
		border: 1rpx solid #FCF1F1;
	}

	.shopnamelist_center:hover {
		background: #FCF1F1;
	}

	.none {
		width: 100%;
		height: 80%;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 36rpx;
	}

	.button {
		border: 2rpx solid;
		border-radius: 20rpx;
		width: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #000;
		margin-top: 30rpx;
		position: relative;
	}

	.btn-bind {
		border: 2rpx solid;
		border-radius: 20rpx;
		width: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #000;
		margin-top: 30rpx;
		position: relative;
	}

	.button:hover,
	.btn-bind:hover {
		background-color: #E96A1D;
		border-color: #E96A1D;
		color: #FFFFFF;
	}

	.cancel-box {
		width: 540rpx;
		height: 280rpx;
		background: #fff;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 20rpx;
	}

	.popup-open {
		font-size: 34rpx;
		text-align: center;
		margin-top: 20rpx;
		color: #000;
	}

	.reason-input {
		border: 1px solid;
		width: 85%;
		margin: 20rpx 0;
		padding: 10rpx;
		color: #000;
		font-size: 30rpx;
	}

	.buttons {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 90%;
		margin-top: 20rpx;
		height: 100%;

		view {
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			height: 90%;
			width: 45%;
			background-color: #EEEEEE;
			border-radius: 40rpx;
		}
	}
</style>